package com.abo.yaas.view.widgets;

import com.abo.yaas.authentication.SessionHandler;
import com.abo.yaas.authentication.UserChangeListener;
import com.abo.yaas.view.View;
import com.abo.yaas.view.ViewContainer;
import com.abo.yaas.view.ViewHandler;
import com.abo.yaas.view.widgets.panels.AuctionFormPanel;
import com.abo.yaas.view.widgets.panels.AuctionTablePanel;
import com.abo.yaas.view.widgets.panels.UserFormPanel;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class MainLayout extends CustomComponent implements ViewContainer {
    private final VerticalLayout layout = new VerticalLayout();

    private final VerticalLayout contents = new VerticalLayout();

    private final HeaderView header = new HeaderView();

    private View currentView;

    public MainLayout() {
        addStyleName("mainlayout");
        setSizeFull();
        layout.setSizeFull();
        layout.setMargin(false);

        layout.addComponent(header);

        contents.setSizeFull();
        layout.addComponent(contents);
        layout.setExpandRatio(contents, 1f);

        ViewHandler.addView(UserFormPanel.class, this);
        ViewHandler.addView(AuctionTablePanel.class, this);
        ViewHandler.addView(AuctionFormPanel.class, this);
        ViewHandler.addView(AuctionView.class, this);

        currentView = ViewHandler.getViewItem(AuctionTablePanel.class).getView();
        contents.addComponent((Component) currentView);
        contents.setComponentAlignment((Component) currentView, Alignment.MIDDLE_CENTER);

        setCompositionRoot(layout);

        SessionHandler.addListener(header);
        SessionHandler.addListener((UserChangeListener) ViewHandler.getViewItem(AuctionView.class).getView());
    }

    @Override
    public void activate(View view) {
        if (!(view instanceof Component)) {
            throw new IllegalArgumentException("View must be a component");
        }
        contents.replaceComponent((Component) currentView, (Component) view);
        currentView = view;
    }

    @Override
    public void deactivate(View view) {

    }
}